package com.stu.boke.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stu.boke.entity.Page;
import com.stu.boke.service.PageService;

@Controller
@RequestMapping(value = "/admin/page")
public class PageController {

	@Autowired
	private PageService pageService;
	
	@RequestMapping("")
	public ModelAndView indexPage() {
		ModelAndView model = new ModelAndView();
		List<Page> pageList = pageService.listPage();
		model.addObject("pageList", pageList);
		model.setViewName("Admin/Page/index");
		return model;
	}
	
	@RequestMapping(value = "/insertSubmit")
	public String insertSubmitPage(Page page) {
		pageService.insertSubmitPage(page);
		return "redirect:/admin/page";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editPageView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Page page = pageService.getPageById(id);
		modelAndView.addObject("page", page);
		modelAndView.setViewName("Admin/Page/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editPageSubmit(Page page) {
		pageService.updatePage(page);
		return "redirect:/admin/page";
	}
	
	@RequestMapping(value = "/insert")
	public ModelAndView insertArticle(Model model) {
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Admin/Page/insert");
        return modelAndView;
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deletePage(@PathVariable("id") Integer id) {
		pageService.deletePage(id);
		return "redirect:/admin/page";
	}
}
